﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace GameNesSL.Controls
{
    using System.Windows.Media.Imaging;
    using System.Windows.Threading;

    public partial class screenshotsscroll : UserControl
    {
        private DispatcherTimer timer;

        public screenshotsscroll()
        {
            InitializeComponent();
            Cursor = Cursors.Hand;

            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(5);
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();

            leftScroll.Completed += new EventHandler(leftScroll_Completed);
            rigthScroll.Completed += new EventHandler(rigthScroll_Completed);
        }

        public bool Mousedown
        {
            get { return mousedown; }
            set
            {
                mousedown = value;
                if (!mousedown && !timer.IsEnabled)
                {
                    timer.Start();
                }
            }
        }

        void timer_Tick(object sender, EventArgs e)
        {
            leftScrollAnim.To = scrollCanvas.ActualWidth - scrollPanel.ActualWidth;
            if (lastdiff < 0)
            {
                leftScroll.Begin();
            }
            else
            {
                rigthScroll.Begin();
            }
            timer.Stop();
        }

        

        void rigthScroll_Completed(object sender, EventArgs e)
        {
            if (keepscroling)
            {
                leftScroll.Begin();
            }
        }

        void leftScroll_Completed(object sender, EventArgs e)
        {
            if (keepscroling)
            {
                rigthScroll.Begin();
            }
        }

        private bool keepscroling = true;
        private double startx = 0;
        private bool mousedown = false;
        private double currentPos;

        private void scrollCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (!Mousedown)
            {
                Mousedown = true;
                startx = e.GetPosition(scrollCanvas).X;
                currentPos = (double)scrollPanel.GetValue(Canvas.LeftProperty);

            }
        }

        public void AddScreenshot(Uri uri)
        {
            Image img = new Image();
            img.Margin = new Thickness(5);
            img.Width = 240;
            img.Stretch = Stretch.Uniform;
            img.Source = new BitmapImage {UriSource = uri};
            scrollPanel.Children.Add(img);
        }

        private void scrollCanvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (Mousedown)
            {
                Mousedown = false;
                ScrollToDiff(e.GetPosition(scrollCanvas).X - startx);
            }
        }

        private double lastdiff = -1;

        private void ScrollToDiff(double diff)
        {
            lastdiff = diff;
            double resultPos = currentPos + diff*3;
            if (resultPos>0)
            {
                resultPos = 0;
            }
            if (resultPos<0)
            {
                if ((resultPos+scrollPanel.ActualWidth)<scrollCanvas.ActualWidth)
                {
                    resultPos = scrollCanvas.ActualWidth - scrollPanel.ActualWidth;
                }            
            }
            scrollAnim.To = resultPos;
            scrollStroyboard.Begin();
        }

        private void scrollCanvas_MouseMove(object sender, MouseEventArgs e)
        {
            if (Mousedown)
            {
                ScrollToDiff(e.GetPosition(scrollCanvas).X - startx);
            }
        }

        private void LayoutRoot_MouseEnter(object sender, MouseEventArgs e)
        {
            Mousedown = false;
        }
    }
}
